<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 模块表 model层
 * @author Toby.Tu 2016-09-02
 */
class Tab_model extends My_Model{
    public $tab = 'tab';
    public $pk = 'id';
    /**
     * 获取当前模块对应的数据信息
     * @author Toby.tu 2017-10-13
     */
    public function init(){
        //获取模块表对应详情数据
        $this->vendor('@.Tab_model');
        $this->tabinfo = $this->tab_model->getTabByName($this->tab);
    }
    /**
     * 获取数据列表
     * @author Toby.tu 2016-09-01
     */
    public function getDataList($search=array(),$current_page=1,$pagesize=0){
        $field = '*';
        $query = $this->select($field);
        $query->where($this->tab.'.deleted',0);
        if(!empty($search)){
            foreach ($search as $fld => $val) {
                if('id' == $fld){
                    $query->where($fld,$val);
                }else{//模糊匹配
                    $query->like($fld,$val);
                }
            }
        }
        $query->order_by($this->tab.'.id desc');
        if(empty($pagesize)){
            $pagesize = $this->pagesize;
        }
        $start = ($current_page-1)*$pagesize;
        $query->limit($pagesize,$start);
        $rows = $this->get();
        if(!empty($rows)){
            return $rows;
        }
        return array();
    }
    /**
     * 获取数据总数
     * @author Toby.tu 2016-09-01
     */
    public function getDataCount($search=array()){
        $query = $this->db->where($this->tab.'.deleted',0);
        $query->order_by($this->tab.'.id desc');
        $cnt = $query->count_all($this->tab);
        return $cnt;
    }
    /**
     * 根据模块名获取模块表数据信息
     * @author Toby.tu 2017-10-13
     */
    public function getTabByName($name=''){
        if(empty($name)) return array();
        $where = array();
        $where['name'] = $name;
        $tabinfo = $this->tab_model->getOne($where);
        if(!empty($tabinfo)){
            return $tabinfo;
        }
        return array();
    }
}
